﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.BasicInfo
{
    public partial class ClassInfo : System.Web.UI.Page
    {
        static int schoolid = Config.CurrentSchoolID;
        static int classid=0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var myClass = from c in db.ClassInfo
                                  select c;
                    this.Store2.DataSource = myClass;
                    this.Store2.DataBind();
                }
            }
        }
        //protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        //{
        //    RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        //    foreach (SelectedRow row in sm.SelectedRows)
        //    {
        //        schoolid =Convert.ToInt32(row.RecordID);
        //    }
        //    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
        //    {
        //        var myShool = from c in db.SchoolInfo
        //                                  where c.SchoolID == schoolid
        //                                  select c.FullName;
        //        foreach (var school in myShool)
        //        {
        //            DropDownField1.Text = school.ToString();
        //        }
        //        var myClass = from c in db.ClassInfo
        //                                  where c.SchoolID == schoolid
        //                                  select c;
        //        this.Store2.DataSource = myClass;
        //        this.Store2.DataBind();
        //    }
        //    schoolid = 0;
        //}

            protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            ClassAliasNameField.Text = "";
            ClassCode.Text = "";
            ClassGrade.Text = "";
            ClassName.Text = "";
            ClassType.Text = "";
            EnrollYear.Text = "2012";
            //schoolid = 0;
            this.FormWindow.Visible = true;
            this.FormWindow.Show();
            Button6.Show();
            Button7.Hide();
        }
            protected void Button3_Click(object sender, Ext.Net.DirectEventArgs e)
            {
                RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
                if(sm.SelectedRows.Count!=0)
                {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    classid = Convert.ToInt32(row.RecordID);
                }
                    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                    {
                        var newclass = from c in db.ClassInfo
                                       where c.ClassID == classid
                                       select c;
                        ExamAnalysis.Model.ClassInfo newc = newclass.First();
                        var school =from s in db.SchoolInfo
                                    where s.SchoolID==newc.SchoolID
                                    select s.FullName;
                        //schoolid = newc.SchoolID;
                        ClassAliasNameField.Text=newc.ClassAliasName;
                        ClassCode.Text=newc.ClassCode;
                        ClassGrade.Text=newc.ClassGrade;
                        ClassName.Text=newc.ClassName;
                        ClassType.Text=newc.ClassType ;
                        EnrollYear.Text=newc.EnrollYear.ToString();
                        //DropDownField2.Text = school.First().ToString();
                    }
                    this.FormWindow.Visible = true;
                    this.FormWindow.Show();
                    Button6.Hide();
                    Button7.Show();
                }
                else
                {
                    X.Msg.Alert("提示信息","请选择一个班级").Show();
                }
            }
            protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
           {
                RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
                if(sm.SelectedRows.Count!=0)
                {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    classid = Convert.ToInt32(row.RecordID);
                }
                    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                    {
                        var newclass = from c in db.ClassInfo
                                       where c.ClassID == classid
                                       select c;
                        ExamAnalysis.Model.ClassInfo newc = newclass.First();
                        db.ClassInfo.DeleteOnSubmit(newc);
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", "删除成功").Show();
                        Response.Redirect("ClassInfo.aspx");
                    }
                }
                else
                {
                    X.Msg.Alert("提示信息", "请选择一个班级").Show();
                }
           }

            //protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
            //{
            //    RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
            //    foreach (SelectedRow row in sm.SelectedRows)
            //    {
            //        schoolid = Convert.ToInt32(row.RecordID);
            //    }
            //    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            //    {
            //        var myShool = from c in db.SchoolInfo
            //                      where c.SchoolID == schoolid
            //                      select c.FullName;
            //        foreach (var school in myShool)
            //        {
            //            DropDownField2.Text = school.ToString();
            //        }
            //    }
            //}

                    protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
                    {
                        //if (DropDownField2.Text.ToString().Equals("请选择"))
                        //{
                        //    X.Msg.Alert("提示信息", String.Format("请选择一个学校")).Show();
                        //}
                        //else
                        //{
                            ExamAnalysis.Model.ClassInfo newc = new ExamAnalysis.Model.ClassInfo();
                            newc.ClassAliasName = ClassAliasNameField.Text;
                            newc.ClassCode = ClassCode.Text;
                            newc.ClassGrade = ClassGrade.Text;
                            newc.ClassName = ClassName.Text;
                            newc.ClassType = ClassType.Text;
                            newc.EnrollYear = Convert.ToInt32(EnrollYear.Text);
                            newc.SchoolID = schoolid;
                            newc.IsValid = true;
                            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                            {
                                db.ClassInfo.InsertOnSubmit(newc);
                                db.SubmitChanges();
                                X.Msg.Alert("提示信息", String.Format("成功添加班级")).Show();
                               // schoolid = 0;
                            }
                            Response.Redirect("ClassInfo.aspx");
                        //}
                    }

                    protected void Change_Click(object sender, Ext.Net.DirectEventArgs e)
                    {
                        using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                        {
                            var newclass = from c in db.ClassInfo
                                           where c.ClassID == classid
                                           select c;
                            ExamAnalysis.Model.ClassInfo newc = newclass.First();
                            newc.ClassAliasName = ClassAliasNameField.Text;
                            newc.ClassCode = ClassCode.Text;
                            newc.ClassGrade = ClassGrade.Text;
                            newc.ClassName = ClassName.Text;
                            newc.ClassType = ClassType.Text;
                            newc.EnrollYear = Convert.ToInt32(EnrollYear.Text);
                            newc.SchoolID = schoolid;
                            db.SubmitChanges();
                            X.Msg.Alert("提示信息", String.Format("成功修改班级信息")).Show();
                           // schoolid = 0;
                            classid = 0;
                        }
                        Response.Redirect("ClassInfo.aspx");
                    }
    }
}